package leetcode.list;

import java.util.List;

public class Lc24 {

    public ListNode swapPairs(ListNode head) {
        if (head == null || head.next == null)
            return head;

        ListNode origin = new ListNode(0, head.next);
        ListNode pre = origin;
        while (head != null && head.next != null) {
            ListNode first = head;
            ListNode second = head.next;
//            ListNode third = null;
//            if (second != null) third = second.next;
            ListNode nextHead = second.next;
            second.next = first;
            first.next = nextHead;
            pre.next = second;
            head = nextHead;
            pre = first;
        }

        return origin.next;
    }
}


class ListNode {
    int val;
    ListNode next;
    ListNode() {}
    ListNode(int val) { this.val = val; }
    ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}